package com.qf.student.web;

import com.alibaba.druid.util.DruidDataSourceUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.qf.student.entity.Emp;
import com.qf.student.service.IEmpService;
import com.qf.student.service.impl.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @version1.0
 * @auth dmh
 */

@WebServlet(urlPatterns = "/Emp")
public class EmpController extends HttpServlet {

    private IEmpService iEmpService = new EmpServiceImpl();

    public EmpController(){

    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 1.判断获取action
        String action = req.getParameter("action");

        // 2.根据action来调用不同的方法
        if ("add".equals(action)) {
            // 执行添加操作
            addStudent(req,resp);
        }else if ("del".equals(action)) {
            del(req,resp);
        }  else if ("update".equals(action)) {
            update(req,resp);
        } else if ("byId".equals(action)) {
            byId(req,resp);
        } else if ("list".equals(action)) {
            list(req,resp);
        }else if("checkName".equals(action)){
            checkName(req,resp);
        }
    }



    private void checkName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String ENAME1 = req.getParameter("ENAME");
        Emp emp = iEmpService.getByName(ENAME1);
        String jsonString = JSON.toJSONString(emp);
        resp.getWriter().write(jsonString);
    }

    private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //1.获取数据
        String id = req.getParameter("EMPNO");

        //类型转换
        int id2 = Integer.parseInt(id);
        //删除
        boolean remove = iEmpService.remove(id2);
        //判断
        if(remove){
            //删除成功回到查询列表
            resp.sendRedirect("/Emp?action=list");

        }else{
            resp.getWriter().write("删除失败!");
        }
    }

    private void list(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        //1.查询数据
        List<Emp> list = iEmpService.list();

        //2.把数据放到作用域中
        req.setAttribute("studentList",list);


        //3.转发到视图转发
        req.getRequestDispatcher("empList.jsp").forward(req,resp);

    }

    private void byId(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {

        //获取传递过来的id
        String id2 = req.getParameter("EMPNO");

        //根据id查询学生信息
        Emp emp = iEmpService.getById(Integer.parseInt(id2));

        //将学生对象方进域对象里面
        req.setAttribute("emp",emp);

        //跳转更新页面
        req.getRequestDispatcher("updateEmp.jsp").forward(req,resp);

    }

    private void update(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        // 1、更新肯定会有参数的，所以需要获取参数
        String empno = req.getParameter("EMPNO");
        String ename = req.getParameter("ENAME");
        String job = req.getParameter("JOB");
        String mgr= req.getParameter("MGR");
        String hiredate = req.getParameter("HIREDATE");
        String sal= req.getParameter("SAL");
        String comm= req.getParameter("COMM");
        String deptno= req.getParameter("deptno");

        // 2、封装到成一个对象
        Emp emp=new Emp();
        emp.setEMPNO(Integer.parseInt(empno));
        emp.setENAME(ename);
        emp.setJOB(job);
        emp.setMGR(Integer.parseInt(mgr));
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date=sdf.parse(hiredate);
            emp.setHIREDATE(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        emp.setSAL(Double.parseDouble(sal));
        emp.setCOMM(Double.parseDouble(comm));
        emp.setDeptno(Integer.parseInt(deptno));
        // 3.调用业务层修改学生数据
        boolean update = iEmpService.update(emp);
        if(update){
            //更新成功就跳转页面到列表(用重定向)
            resp.sendRedirect("Emp?action=list");
        }else{
            resp.getWriter().write("更新失败");
        }

    }

    private Emp studentById(HttpServletRequest req) {
            String EMPNO = req.getParameter("EMPNO");
            return iEmpService.getById(Integer.parseInt(EMPNO));
        }


    private void addStudent(HttpServletRequest req,HttpServletResponse resp) throws IOException {

        // 1、添加肯定会有参数的，所以需要获取参数
        String empno = req.getParameter("EMPNO");
        String ename = req.getParameter("ENAME");
        String job = req.getParameter("JOB");
        String mgr= req.getParameter("MGR");
        String hiredate = req.getParameter("HIREDATE");
        String sal= req.getParameter("SAL");
        String comm= req.getParameter("COMM");
        String deptno= req.getParameter("deptno");

        // 2、封装到成一个对象
        Emp emp=new Emp();
        emp.setEMPNO(Integer.parseInt(empno));
        emp.setENAME(ename);
        emp.setJOB(job);
        emp.setMGR(Integer.parseInt(mgr));
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date=sdf.parse(hiredate);
            emp.setHIREDATE(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        emp.setSAL(Double.parseDouble(sal));
        emp.setCOMM(Double.parseDouble(comm));
        emp.setDeptno(Integer.parseInt(deptno));
        // 3.调用业务层添加学生
        boolean save = iEmpService.save(emp);
        // 根据添加的结果响应用户
        if (save) {
            //如果添加成功了就展示页面给用户看,跳转到列表页面
//                resp.getWriter().write("添加成功");
            resp.sendRedirect("Emp?action=list");//重定向是两次请求
        } else {
            resp.getWriter().write("添加失败");
        }
    }
}